#ifndef _COMMON_DEFS_H
#define _COMMON_DEFS_H

#include <nds.h>

#include <sys/stat.h>

// video defines
#define SUB ((u16 *)BG_BMP_RAM_SUB(2))
#define MAIN ((u16 *)BG_BMP_RAM(2))

// general defines
#ifndef MAX
#define MAX(a, b) ((a)<(b)?(b):(a))
#endif
#ifndef MIN
#define MIN(a, b) ((a)>(b)?(b):(a))
#endif

// an extension list
struct sEL {
    char **exts;
    u32 extnum;
};

// a picture
struct sPicture {
    s16 height, width;
    u8 bpp;
    u16* palette;

    union {
        u8* u8;
        u16* u16;
        u32* u32;
    } data;
};

// a file (regular or not)
struct sFile {
    struct sFile *prev;
    struct sFile *next;

    char name[256];
    union {
        u32 nameh;
        struct {
            u16 namel;  // name length
            u16 _nameh; // name hash
        };
    };

    struct stat st;
};

// <-- instance system
    // local keys
    #if defined(Plugin)
    #   define scanKeys()           scanKeys        (&_pinstance.keys)
    #   define keysHeld()           keysHeld        (&_pinstance.keys)
    #   define keysDown()           keysDown        (&_pinstance.keys)
    #   define keysDownRepeat()     keysDownRepeat  (&_pinstance.keys)
    #   define keysSetRepeat(d, r)  keysSetRepeat   (&_pinstance.keys, (d), (r))
    #   define keysUp()             keysUp          (&_pinstance.keys)
    #endif

    // a keys instance
    struct sKeysInstance {
        u16 keys;
        u16 keysold;
        u16 keysrepeat;

        u8 delay;
        u8 repeat;
        u8 count;
    };

    // a plugin instance
    struct sPInstance {
        int err;

        sKeysInstance keys;
    };
// -->

// <-- music system
    #define S_STATES_MASK 3
    enum S_STATES {
        S_STOPPED,
        S_PAUSED,
        S_PLAYING
    };

    // a sound
    struct sSInfos {
        u8 channels;
        u8 samplesize;
        u32 samplingrate;
        u32 duration;
        u32 position;
        u32 flags;    // state
        FILE *fd;
    };

    // tags of a music file
    struct sSTags {
        char *title;
        char *author;
        char *album;
        u8 track;
        u16 year;
        char *genre;
        char *comment;
        // ...
    };
// -->

#endif

